<?php
use \GatewayWorker\Lib\Gateway;


class startbattle extends cmdbase
{
	public function onCmd($msg){

		$group_id = $this->getCurGroupId();
		$num = Gateway::getClientCountByGroup($group_id);
		if($num < 2){
			$msg_body = array(
            	'ret'=> NetErrorCode::$Error,
            	"reason" => "至少2个人才能开始",
        	);
 			return $msg_body;
		}

		$group_mgr = $GLOBALS['GroupMgr'];
		$client_mgr = $GLOBALS['ClientMgr'];

		//广播
		$msg_person = array('persons'=> array());
		$uid_list = Gateway::getUidListByGroup($group_id);
		foreach ($uid_list as $uid) {
      		$name = $client_mgr->getNameByUid($uid);
			$msg_person['persons'][] = array('uid'=>$uid, 'name'=>$name, 'state'=>2, 'content'=>"1,1,1,1,1");//state = 2是 客户端的open状态
		}
		$ret = $this->sendToGroup("notify_enterbattle", $msg_person);

 		$msg_body = array();
		if($ret){
			$msg_body['ret'] = NetErrorCode::$None;
			$msg_body['reason'] = "success";

			//标志此房间已经开始战斗，不能随机匹配上
			$group_mgr->startBattle($group_id, $msg_person);
		}else{
			$msg_body['ret'] = NetErrorCode::$Error;
			$msg_body['reason'] = "Fatal Error:group is miss";
		}

 		return $msg_body;
	}
}